<template>
  <div>
    <div id="line" style="width: 100%; height: 0px ;margin-top: -20px"></div>
    <div style="height: 20px"></div>
    <el-row>
      <el-button size="small" type="primary" icon="CirclePlus" @click="handle_add()">新增</el-button>
    </el-row>
    <!--          |右侧主体内容|-->
    <!--          列属性-->
    <el-card style="margin-top:10px">
      <el-table :data="tableData">
        <el-table-column prop="name" label="菜品名称" width="120"></el-table-column>
        <el-table-column prop="material" label="菜品原料"></el-table-column>
        <el-table-column prop="price" label="菜品价格"></el-table-column>
        <el-table-column prop="popularity" label="菜品热度"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini"  type="success" icon="el-icon-edit"  @click="handle_update(scope.row)">修改</el-button>
            <el-button size="mini"  type="danger" icon="el-icon-delete" @click="handle_delete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--          分页组件-->
    <div style="padding: 10px 0;text-align: center;margin-top: 20px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--          点击"新增"按钮时，显示嵌套表格的"弹窗"-->
    <el-dialog title="菜品信息" v-model="dialogFormVisible_01" width="30%">
      <el-form >
        <el-form-item>
          <el-select v-model="form.type" placeholder="请选择菜品类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品名称"  label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜品原料"  label-width="80px">
          <el-input v-model="form.material" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜品价格"  label-width="80px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible_01 = false">取 消</el-button>
        <el-button type="primary" @click="handle_add_save">确 定</el-button>
      </div>
    </el-dialog>
    <!--          点击"修改"按钮时，显示嵌套表格的"弹窗"-->
    <el-dialog title="菜品信息" v-model="dialogFormVisible_02" width="30%">
      <el-form >
        <el-form-item>
          <el-select v-model="form.type" placeholder="请选择菜品类型" :disabled="true">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品名称"  label-width="80px">
          <el-input v-model="form.name" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="菜品原料"  label-width="80px">
          <el-input v-model="form.material" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜品价格"  label-width="80px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible_02 = false">取 消</el-button>
        <el-button type="primary" @click="handle_update_save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from "@/utils/request";
import * as echarts from 'echarts';

const option = {
  title: {
    text: 'beef'
  },
  xAxis: {              //x轴数据
    type: 'category',
    data: []
  },
  yAxis: {              //y轴数据
    type: 'value'
  },
  series: [             //每有一条折线记录，就需要登记一个series记录，不止一条折线时每个series记录都需要一个name属性以区别
    {
      data: [],
      type: 'line',
      smooth: true
    }
  ]
};

export default {
  name: "P_pork",
  data() {
    return{
      type: 1,
      lineBox: null,      //定义echarts容器
      // 关于菜品的增删改
      tableData:[],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      dialogFormVisible_01: false,           //"新增"弹窗属性，默认不展示
      dialogFormVisible_02: false,           //"修改"弹窗属性，默认不展示
      form: {},
      options: [
        {
          value: 0,
          label: '猪肉类'
        }, {
          value: 1,
          label: '牛肉类'
        }, {
          value: 2,
          label: '羊肉类'
        }, {
          value: 3,
          label: '蔬菜类'
        }, {
          value: 4,
          label: '水产类'
        },{
          value: 5,
          label: '豆制品'
        },{
          value: 6,
          label: '鸡蛋类'
        },{
          value: 7,
          label: '主食类'
        }],
    }
  },
  mounted() {             //不同于create函数,当页面容器初始化都完成之后才开始调用mounted函数
    this.load();
    this.load_dish();
  },
  methods: {
    load(){
      if(!this.lineBox){
        //初始化echarts容器，之后lineBox就不再为null
        this.lineBox = echarts.init(document.getElementById("line"))
      }
      request.get("/admin/popularity",{
        params:{
          type: this.type
        }
      }).then(res=>{
        console.log(res.content);
        option.xAxis.data = res.content.dish;
        option.series[0].data = res.content.popularity;
        this.lineBox.setOption(option);
      })
    },
    load_dish(){
      // 请求分页相关数据
      request.get("/client/dish/select",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          type: 1,
          name:""
        }
      }).then(res =>{
        console.log(res.content);
        this.tableData = res.content.data;
        this.total = res.content.total;
      })
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum;
      this.load_dish();
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize;     //监听页面变化
      this.load_dish();                //页面变化后重新加载数据
    },

    handle_add(){
      this.dialogFormVisible_01 = true;
      this.form = {};
    },
    handle_add_save(){
      request.post("/admin/save",this.form).then(res =>{
        if(res.success){
          this.dialogFormVisible_01 = false;
          this.load();
          this.load_dish()
          this.$message.success("新增成功！");
        }
        else {
          this.$message.error("新增失败！该部门编号已存在。");
          this.dialogFormVisible_01 = false;
        }
      })
    },

    handle_update(row){
      this.form = row;
      this.dialogFormVisible_02 = true;
    },

    handle_update_save(){
      request.post("/admin/save",this.form).then(res =>{
        if(res.success){
          this.dialogFormVisible_02 = false;
          this.load_dish();
          this.$message.success("修改成功！");
        }
        else {
          this.$message.error("修改失败！");
          this.dialogFormVisible_02 = false;
        }
      })
    },

    handle_delete(row){
      this.form = row;
      this.$confirm('此操作将永久删除该菜品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handle_delete_confirm();
        this.load_dish();
        this.$message.success("删除成功！");
      }).catch(() => {
        this.$message.info("已取消删除。");
      })
    },

    handle_delete_confirm(){
      request.post("/admin/delete",this.form);
    },

  }

}

</script>

<style scoped>

</style>